package com.xiaodou.mjf.application.core.utils;

import java.math.BigDecimal;

/**
 * Created by Rocky on 15/3/20.
 */
public class LoanUtils {

    /**
     * 计算每月应还金额(等额本息)
     * @param loanBalance   贷款本金
     * @param insterestRate 年利率
     * @param period        期数
     * @return              每期应还款
     */
    public static Double calculateMonthlyRepaymentBalance(Double loanBalance,Double insterestRate,Integer period){

        Double monthlyInterestRate = insterestRate / 12;

        Double monthlyBalance = loanBalance * monthlyInterestRate * (Math.pow(1 + monthlyInterestRate,period)) / (Math.pow(1 + monthlyInterestRate,period) -1);
        BigDecimal bigDecimal = new BigDecimal(monthlyBalance);
        return bigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /***
     * 计算每月应付利息
     * @param loanBalance       贷款本金
     * @param insterestRate     年利率
     * @param period            贷款期数
     * @param currentPeriod     当前期数
     * @return                  每月应付利息
     */
    public static Double calculateMonthlyInsterstBalance(Double loanBalance,Double insterestRate,Integer period,Integer currentPeriod){

        Double monthlyInterestRate = insterestRate / 12;
        double monthlyInterestBalance = loanBalance * monthlyInterestRate * (Math.pow((1 + monthlyInterestRate), period) - Math.pow((1 + monthlyInterestRate), (currentPeriod - 1))) / (Math.pow((1 + monthlyInterestRate), period) - 1);
        BigDecimal bigDecimal = new BigDecimal(monthlyInterestBalance);
        return bigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();

    }
}
